package com.wjy.common.config.redis;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author ybxxszl
 * @desc Redis配置
 * @date 2019/4/14
 */
@Configuration
public class RedisConfig implements EnvironmentAware {

    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public Jedis getJedis() {
        Jedis jedis = getJedisPool().getResource();
        return jedis;
    }

    public JedisPool getJedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(Integer.valueOf(environment.getProperty("spring.redis.jedis.pool.min-idle")));
        jedisPoolConfig.setMaxIdle(Integer.valueOf(environment.getProperty("spring.redis.jedis.pool.max-idle")));
        jedisPoolConfig.setMaxWaitMillis(Integer.valueOf(environment.getProperty("spring.redis.jedis.pool.max-wait")));
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, environment.getProperty("spring.redis.host"),
                Integer.valueOf(environment.getProperty("spring.redis.port")),
                Integer.valueOf(environment.getProperty("spring.redis.timeout")),
                environment.getProperty("spring.redis.password"),
                Integer.valueOf(environment.getProperty("spring.redis.database")));
        return jedisPool;
    }

    // @Bean
    // @ConditionalOnMissingBean(name = "redisTemplate")
    // public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
    // RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
    // redisTemplate.setEnableTransactionSupport(true);
    // redisTemplate.setConnectionFactory(factory);
    // StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    // redisTemplate.setKeySerializer(stringRedisSerializer);
    // redisTemplate.setHashKeySerializer(stringRedisSerializer);
    // Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
    // new Jackson2JsonRedisSerializer<Object>(Object.class);
    // redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    // redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    // redisTemplate.afterPropertiesSet();
    // return redisTemplate;
    // }
    //
    // @Bean
    // @ConditionalOnMissingBean(name = "stringRedisTemplate")
    // public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
    // StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    // stringRedisTemplate.setEnableTransactionSupport(true);
    // stringRedisTemplate.setConnectionFactory(factory);
    // StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    // stringRedisTemplate.setKeySerializer(stringRedisSerializer);
    // stringRedisTemplate.setHashKeySerializer(stringRedisSerializer);
    // Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
    // new Jackson2JsonRedisSerializer<Object>(Object.class);
    // stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    // stringRedisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    // stringRedisTemplate.afterPropertiesSet();
    // return stringRedisTemplate;
    // }

}
